﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
// NOM Library
using NomCafe;



namespace NomCafe
{
    static class Program
    {
        private static SplashScreen splash;
        public static string NomCafeVersion = "1.0.0.0 BETA";
        public static Process checkInstance()
        {
            // Returns a System.Diagnostics.Process pointing to
            // a pre-existing process with the same name as the
            // current one, if any; or null if the current process
            // is unique.
            Process curr = Process.GetCurrentProcess();
            Process[] procs = Process.GetProcessesByName(curr.ProcessName);
            foreach (Process p in procs)
            {
                if ((p.Id != curr.Id) &&
                    (p.MainModule.FileName == curr.MainModule.FileName))
                    return p;
            }
            return null;
        }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (checkInstance() != null)
            {
                MessageBox.Show("Chương trình này đang chạy ! Vui lòng kiểm tra lại.","NOM Cafe",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            #region " Setup and display splash screen "
            var InfoLabel = new Point(260, 190); // Creates the point to position the ProgramInfoLabel
            var StatusLabel = new Point(40, 248); // Creates the point to position the StatusLabel

            splash = SplashScreen.Current; // 
            splash.SetTransparentKey = Color.Fuchsia;

            splash.SetFade = true;  // This fades the splash screen in and out. However, when using TransparencyKey, doesn't not display correctly.

            splash.SetInfoLabel(InfoLabel, 499, 117);
            splash.SetStatusLabel(StatusLabel, 490, 17);
            splash.ShowSplashScreen();

            splash.SetInfo = "Phiên bản " + NomCafeVersion + ".\nWebsite:http://nom.vn\nNhóm phát triển: NHTU Group";
             
            
            //Create and AssemblyHandler
            AppDomain currentDomain = AppDomain.CurrentDomain;
            currentDomain.AssemblyLoad += asmLoadHandler;


            // Load Nom config and startup
            if (!LoadConfigData())
            {
                Application.Exit();
                return;
            }

            #endregion

            Application.Run(new Main());

        }
        /***************************************
         * Get Resource Stream
         * return object resource name || null value
         * 
         **************************************/
        private static System.IO.Stream GetResourceStream(String resource)
        {
            var ea = System.Reflection.Assembly.GetExecutingAssembly();
            foreach (String curResource in ea.GetManifestResourceNames())
            {
                if (curResource.EndsWith(resource))
                {
                    return ea.GetManifestResourceStream(curResource);
                }
            }
            return null;
        }

        static void asmLoadHandler(object sender, AssemblyLoadEventArgs args)
        {
            // This probably won't display anything for the demo because there is hardly anything to load.
            splash.SetStatus = "Cập nhật thư viện: " + args.LoadedAssembly.GetName().Name.ToString() + " ...";
        }

        public static bool LoadConfigData()
        {
            // Simulate loading...
            // This would not normally be done here, but rather in the Form_Load, etc etc
            /*
            // Check Internet connection
            splash.SetStatus = "Kiểm tra kết nối Internet ...";
            if ( ! CheckConnection() )
            {
                MessageBox.Show("Kết nối đến server thất bại. Vui lòng kiểm tra lại.");
                return false;
            }
            Thread.Sleep(1000);
            */
            // Check version
            splash.SetStatus = "Kiểm tra phiên bản ...";
            Thread.Sleep(1000);

            //Check sum file
            splash.SetStatus = "Kiểm tra file ...";
            Thread.Sleep(900);

            //Auto update NomCafe
            splash.SetStatus = "Cập nhật phiên bản ...";
            Thread.Sleep(1500);
            
            return true;
        }
        /***************************************
         * Check Internet Connection
         * return Boolean value
         * 
         **************************************/
        static bool CheckConnection()
        {
            string siteConnect = "www.google.com";
            int portConnect = 80;
            try
            {
                System.Net.Sockets.TcpClient clnt = new System.Net.Sockets.TcpClient(siteConnect, portConnect);
                clnt.Close();
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
        }
    }
}
